package com.itheima.service.db.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.AnalysisByDay;
import com.itheima.mapper.AnalysisByDayMapper;
import com.itheima.mapper.LogMapper;
import com.itheima.service.db.AnalysisByDayService;
import com.itheima.util.ComputeUtil;
import com.itheima.vo.AnalysisSummaryVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;

@DubboService
public class AnalysisByDayServiceImpl implements AnalysisByDayService {

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    @Override
    public void LogToAnalysisByDay() {
        // 1.查询四项数据
        // 1-1 今天时间
        String today = DateUtil.offsetDay(new Date(), 0).toDateStr();
        // 1-2 昨天时间
        String yesterday = DateUtil.offsetDay(new Date(), -1).toDateStr();
        // 1-3 查询今日注册
        Integer numRegistered = logMapper.findTypeAndTime("0102", today);
        // 1-4 查询今日活跃
        Integer numActive = logMapper.findByTime(today);
        // 1-5 查询今日登录
        Integer numLogin = logMapper.findTypeAndTime("0101", today);
        // 1-6 查询次日留存
        Integer numRetention1d = logMapper.findByTodayAndYesterday(today, yesterday);

        // 2.保存到每日分析表
        // 2-1 先查今天有没有数据
        QueryWrapper<AnalysisByDay> qw = new QueryWrapper<>();
        qw.eq("record_date", today);
        AnalysisByDay analysisByDay = analysisByDayMapper.selectOne(qw);
        if (analysisByDay == null) {
            // 2-2 新增
            analysisByDay = new AnalysisByDay();
            analysisByDay.setRecordDate(new Date());
            analysisByDay.setNumRegistered(numRegistered);
            analysisByDay.setNumActive(numActive);
            analysisByDay.setNumLogin(numLogin);
            analysisByDay.setNumRetention1d(numRetention1d);
            // 这里调用insert方法
            analysisByDayMapper.insert(analysisByDay);

        } else {
            // 2-3 修改
            analysisByDay.setNumRegistered(numRegistered);
            analysisByDay.setNumActive(numActive);
            analysisByDay.setNumLogin(numLogin);
            analysisByDay.setNumRetention1d(numRetention1d);
            // 这里调用updateById方法
            analysisByDayMapper.updateById(analysisByDay);
        }

    }

    // 查询首页统计信息
    @Override
    public AnalysisSummaryVo findAnalysisSummaryVo() {
        // 1.准备基础数据
        // 1-1 今天
        String today = DateUtil.offsetDay(new Date(), 0).toDateStr();
        // 1-2 昨天
        String yesterday = DateUtil.offsetDay(new Date(), -1).toDateStr();
        // 1-3 前7天
        String passWeek = DateUtil.offsetDay(new Date(), -7).toDateStr();
        // 1-4 前30天
        String passMonth = DateUtil.offsetDay(new Date(), -30).toDateStr();
        // 1-5 今日分析数据
        AnalysisByDay todayAnalysisByDay = analysisByDayMapper.selectOne(new QueryWrapper<AnalysisByDay>().eq("record_date", today));
        // 1-6 昨日分析数据
        AnalysisByDay yesterdayAnalysisByDay = analysisByDayMapper.selectOne(new QueryWrapper<AnalysisByDay>().eq("record_date", yesterday));

        // 2.凑齐9份数据
        // 2-1 累计用户数
        Long cumulativeUsers = analysisByDayMapper.findCumulativeUsers();
        // 2-2 过去30天活跃用户数
        Long activePassMonth = analysisByDayMapper.findActivePassCondtion(passMonth, today);
        // 2-3 过去7天活跃用户
        Long activePassWeek = analysisByDayMapper.findActivePassCondtion(passWeek, today);
        // 2-4 日新增用户数量
        long newUsersToday = todayAnalysisByDay.getNumRegistered().longValue();
        // 2-5 日新增用户涨跌率，单位百分数，正数为涨，负数为跌   BigDecimal : 商业数字格式
        BigDecimal newUsersTodayRate = ComputeUtil.computeRate(todayAnalysisByDay.getNumRegistered(), yesterdayAnalysisByDay.getNumRegistered());
        // 2-6 今日登录次数
        long loginTimesToday = todayAnalysisByDay.getNumLogin().longValue();
        // 2-7 今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        BigDecimal loginTimesTodayRate = ComputeUtil.computeRate(todayAnalysisByDay.getNumLogin(), yesterdayAnalysisByDay.getNumLogin());
        // 2-8 今日活跃用户数量
        long activeUsersToday = todayAnalysisByDay.getNumActive().longValue();
        // 2-9 今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        BigDecimal activeUsersTodayRate = ComputeUtil.computeRate(todayAnalysisByDay.getNumActive(), yesterdayAnalysisByDay.getNumActive());

        // 3.封装vo
        AnalysisSummaryVo vo = new AnalysisSummaryVo();
        vo.setCumulativeUsers(cumulativeUsers);
        vo.setActivePassMonth(activePassMonth);
        vo.setActivePassWeek(activePassWeek);
        vo.setNewUsersToday(newUsersToday);
        vo.setNewUsersTodayRate(newUsersTodayRate);
        vo.setLoginTimesToday(loginTimesToday);
        vo.setLoginTimesTodayRate(loginTimesTodayRate);
        vo.setActiveUsersToday(activeUsersToday);
        vo.setActiveUsersTodayRate(activeUsersTodayRate);
        return vo;
    }
}
